package me.ywd.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author Ye_Wenda
 * @Date 8/7/2017
 */
@RestController
@RequestMapping("consume")
public class ConsumerDemoController {
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("")
    public String consumeGet() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("provider");
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/" + "services";
        System.out.println(serviceInstance.getUri().toString());
        String result = restTemplate.getForObject(url, String.class);
        return result;
    }
}
